unit viewTPS;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection,Unit_External, Setting, TPS_Edit, TPS_Tambah,
  Tabs,ComCtrls, TPS_TambahN, frxpngimage, ExtCtrls;

type
  TTPS = record
     ID:String;
     NAMA:String;
  end;
  TTTPS = array of TTPS;
  TPenyimpan = record
     ID:String;
     NAMA:String;
  end;
  TTPenyimpan = array of TPenyimpan;
  TForm4 = class(TForm)
    btnHapus: TButton;
    sgTPS: TStringGrid;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    ZQuery2: TZQuery;
    ZTable2: TZTable;
    GroupBox3: TGroupBox;
    sgPenduduk: TStringGrid;
    btnEdittps: TButton;
    btnTambahTPS: TButton;
    btnPrev: TButton;
    btnNext: TButton;
    txtHal: TEdit;
    lblHal: TLabel;
    lblNTPS: TLabel;
    lblPemilihTPS: TLabel;
    txtHal2: TEdit;
    btnPrev2: TButton;
    btnNext2: TButton;
    lblHal2: TLabel;
    lblPemilihTotal: TLabel;
    lblPemilihNonTPS: TLabel;
    GroupBox2: TGroupBox;
    Button1: TButton;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    Label2: TLabel;
    Label1: TLabel;
    cbKabkot: TComboBox;
    cbKec: TComboBox;
    cbDesa: TComboBox;
    btnRefresh: TButton;
    Image2: TImage;
    Image1: TImage;
    Button2: TButton;
    ZQuery3: TZQuery;
    Button3: TButton;
    procedure btnHapusClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cbKabkotChange(Sender: TObject);
    procedure cbKecChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);
    procedure BtnprevClick(Sender: TObject);
    procedure btnnextClick(Sender: TObject);
    procedure txtHalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PrintTpsClick(Sender: TObject);
    procedure btnEdittpsClick(Sender: TObject);
    procedure btnTambahTPSClick(Sender: TObject);
    procedure sgTPSClick(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure btnPrev2Click(Sender: TObject);
    procedure btnNext2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    procedure TampilHalaman(Hal: Integer = 1);
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure getData;
    procedure getPage;
    procedure ResetMe;
    procedure tampilsgpenduduk(Hal: Integer = 1);
    function getIdsimpan(tabel: String): String;
    function getSimpan(id: String): String;
    procedure getPenduduk;
    { Private declarations }
  public
    { Public declarations }
    ext:ExternalFile;
    constructor Create(AOwner: TComponent; kodelog:String); overload;
  end;

var
  Form4: TForm4;
  editTps: TeditTps;
  tambahtps: Ttambahtps;
  tambahntps:TForm_TambahTPS;
  sKabkot,skabkot2:string;
  skec,skec2:string;
  sdesa,sdesa2,skodekab,skodekec:string;
  tps,namatps:string;
  crow,crow2:integer;
  drow,drow2:integer;
  bpenyimpan:TTPenyimpan;
  cHal,chal2:Integer; //current halaman
  jHal,jhal2:Integer; //jumlah halaman
  bHal:Integer; //batas per halaman
  bTPS:TTTPS; //grid 1
  ct,ct2:Integer;
  ptable:String;
  akec:array of String;
  pid:String;

implementation

{$R *.dfm}

constructor TForm4.Create(AOwner: TComponent; kodelog:String);
begin
  if(length(kodelog)>2)then
  begin
    skodekab:=copy(kodelog,1,2);
    skodekec:=kodelog;
  end
  else
  begin
    skodekab:=kodelog;
    skodekec:='';
  end;
  //skode:=kodelog;
  inherited Create(AOwner);
  BorderIcons := BorderIcons - [biMaximize];
end;

procedure TForm4.btnEdittpsClick(Sender: TObject);
var
  id,jml:String;
begin
  //showmessage(btps[sgTPS.Row].ID);
  editTps := TeditTps.Create(Application,btps[sgTPS.Row].ID);
end;

procedure TForm4.btnHapusClick(Sender: TObject);
var
  query:String;
  buttonSelected : Integer;
begin
  buttonSelected := MessageDlg('Apakah anda yakin akan apakah anda yakin akan menghapus '+btps[sgTPS.Row].NAMA+'?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    with ZQuery1 do begin
      ZQuery3.active:=false;
      Zquery3.SQL.Clear;
      Zquery3.SQL.Text:='SELECT NAMA FROM penyimpan WHERE NAMA LIKE '+quotedstr('pemilih_'+skec+'%');
      Zquery3.Active:=true;
      zquery3.first;
      while not zquery3.Eof do begin
      Active:=False;
      SQL.Clear;
      query:='UPDATE '+ZQuery3.Fields[0].AsString+' SET TPS=0 WHERE TPS='+btps[sgTPS.Row].ID;
  //    ShowMessage(query);
      sql.Text:=query;
      ExecSQL;
      ZQuery3.Next;
      end;
      //
      Active:=False;
      SQL.Clear;
      query:='DELETE FROM daftar_tps WHERE ID_TPS='+btps[sgTPS.Row].ID;
  //    ShowMessage(query);
      sql.Text:=query;
      ExecSQL;
      ShowMessage('TPS '+btps[sgTPS.Row].NAMA+' berhasil dihapus');
      getpenduduk;
      if (ct2>=0) then tampilsgpenduduk(1);
      //
      getData;
      if (ct>=0) then tampilhalaman(1);
      //
      btnTambahTPS.Enabled:=True;
    end;
  end;
end;

procedure TForm4.btnNext2Click(Sender: TObject);
begin
  tampilsgpenduduk(cHal2+1);
  txtHal2.Text:=IntToStr(cHal2);
end;

procedure TForm4.PrintTpsClick(Sender: TObject);
var
  query:String;
begin
  with ZTable1 do begin
    Active:=False;
    TableName:='pemilih_'+sdesa;
    Filter:='TPS='+btps[sgTPS.Row].ID;
    Filtered:=true;
    active:=true;
    First;
//    ext.exportExcel(ztable1);
  end;
end;

procedure TForm4.ResetMe();
var
  i:Integer;
  query:String;
begin
  for i := 1 to sgTPS.RowCount - 1
  do sgTPS.Rows[i].Clear;
  for i := 1 to sgPenduduk.RowCount - 1
  do sgPenduduk.Rows[i].Clear;
  jHal:=0;
  ct:=0;
  SetLength(bTPS,0);
  btnHapus.Enabled:=False;
  btnEdittps.Enabled:=False;
  btnTambahTPS.Enabled:=False;
//  printTPS.Enabled:=False;
  btnPrev.Enabled:=false;
  btnNext.Enabled:=false;
end;

procedure TForm4.sgTPSClick(Sender: TObject);
begin
  if (sgTPS.Row>0) and (sgTPS.Cells[1,1]<>'') then begin
    btnHapus.Enabled:=True;
    btnEdittps.Enabled:=True;
//    printTPS.Enabled:=True;
  end
    else
  begin
    btnHapus.Enabled:=false;
    btnEdittps.Enabled:=false;
//    printTPS.Enabled:=True;
  end;
end;

procedure TForm4.btnnextClick(Sender: TObject);
begin
  TampilHalaman(cHal+1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TForm4.btnPrev2Click(Sender: TObject);
begin
  tampilsgpenduduk(cHal2-1);
  txtHal2.Text:=IntToStr(cHal2);
end;

procedure TForm4.BtnprevClick(Sender: TObject);
begin
  TampilHalaman(cHal-1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TForm4.btnTambahTPSClick(Sender: TObject);
begin
//  tambahtps := Ttambahtps.Create(Application,sdesa);
  tambahntps:=TForm_TambahTPS.Create(Application,sdesa,1);
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm4.btnRefreshClick(Sender: TObject);
begin
  if getIdsimpan('pemilih_'+sdesa)<>'' then begin
      getpenduduk;
      if (ct2>=0) then tampilsgpenduduk(1);
      //
      getData;
      if (ct>=0) then tampilhalaman(1);
      //
      btnTambahTPS.Enabled:=True;
      button2.enabled:=true;
    end
end;

procedure TForm4.cbDesaChange(Sender: TObject);
var
  desa:String;
  query:string;
  kode:string;
begin
  if (cbDesa.ItemIndex > 0) then begin
    desa := cbDesa.Items[cbDesa.ItemIndex];
    //
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(desa)+' AND KODE_KECAMATAN = '+QuotedStr(skec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sDesa := ZTable1.FieldByName('KODE_DESA').AsString;
    if getIdsimpan('pemilih_'+sdesa)<>'' then begin
      btnRefresh.Enabled:=true;
      getpenduduk;
      if (ct2>0) then tampilsgpenduduk(1);
      //
      getData;
      if (ct>0) then tampilhalaman(1);
      //
      btnTambahTPS.Enabled:=True;
      button2.Enabled:=true;
    end
      else
    begin
      ShowMessage('Desa ini belum terdapat');
      btnRefresh.Enabled:=false;
    end;
  end
    else
  begin
    sDesa:='';
    btnHapus.Enabled:=False;
    btnEdittps.Enabled:=False;
    btnTambahTPS.Enabled:=False;
//    printTPS.Enabled:=False;
  end;
end;

procedure TForm4.getData();
var
  i:Integer;
  que:String;
begin
    //get TPS count
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      //que:='SELECT count(*) FROM (SELECT DISTINCT(TPS) FROM `pemilih_'+sdesa+'` WHERE TPS>0) as tes RIGHT JOIN daftar_tps ON tes.TPS=daftar_tps.ID_TPS';
      que:='SELECT count(*) FROM daftar_tps where KODE_DESA='+QuotedStr(sdesa);
//      ShowMessage('q3='+que);
      sql.Text:=que;
      Active:=true;
      first;
      SetLength(bTPS,Fields[0].AsInteger+1);
      lblNTPS.Caption:='Jumlah TPS : '+Fields[0].AsString;
    end;
    ct:=ZQuery1.Fields[0].AsInteger;
    jHal:=ct div bHal;
    if ((ct mod bHal)>0) then jHal:=jHal+1;
    //jtps
    {with ZQuery1 do begin
      active:=false;
      sql.Clear;
      //que:='SELECT count(distinct(TPS)) FROM pemilih_'+sdesa+' WHERE TPS>0';
      que:='SELECT count(*) FROM daftar_tps where KODE_DESA='+quotedstr(sdesa)+'';
      sql.Text:=que;
      Active:=true;
      first;
      lblNTPS.Caption:='Jumlah TPS : '+Fields[0].AsString;
    end;}
    //jpemilihtps
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      que:='SELECT count(*) FROM pemilih_'+sdesa+' WHERE TPS>0 AND STATUS2=''0''';
//      showmessage(que);
      sql.Text:=que;
      Active:=true;
      first;
      lblPemilihTPS.Caption:='Jumlah Pemilih yang Sudah Terdaftar TPS: '+Fields[0].AsString;
    end;
end;

procedure TForm4.TampilHalaman(Hal:Integer = 1);
var
  i,awal, jmlint:Integer;
  que,nama,jml,alamat,idtps:String;
  namtab:string;
begin
  dRow2:=1;
  crow2:=2;
  cHal:=Hal;
  sgTPS.RowCount:=crow2;
  lblHal.Caption:='-';
  awal:=(Hal-1)*bHal;
  //reset
  for i := 1 to sgTPS.RowCount - 1
  do sgTPS.Rows[i].Clear;
  //
  with ZQuery1 do begin
    active:=false;
    sql.Clear;
    //que:='SELECT * FROM (SELECT DISTINCT(TPS) FROM `pemilih_'+sdesa+'` WHERE TPS>0) as tes LEFT JOIN daftar_tps ON tes.TPS=daftar_tps.ID_TPS ORDER BY daftar_tps.ID_TPS ASC LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
    que:='SELECT * FROM daftar_tps where KODE_DESA='+quotedstr(sdesa)+' ORDER BY NAMA+0 ASC LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
//    ShowMessage('q2='+que);
    sql.Text:=que;
    Active:=true;
    first;
  end;
  while not zquery1.Eof do begin
    nama:=zquery1.FieldByName('NAMA').AsString;
    alamat:=zquery1.FieldByName('ALAMAT').AsString;
    idtps:=zquery1.FieldByName('ID_TPS').AsString;
    //cari tabel dalam kecamatan
    jmlint := 0;
    ZQuery3.active:=false;
    Zquery3.SQL.Clear;
    Zquery3.SQL.Text:='SELECT NAMA FROM penyimpan WHERE NAMA LIKE '+quotedstr('pemilih_'+skec+'%');
    Zquery3.Active:=true;
    zquery3.first;
    while not zquery3.Eof do begin
    //ganti
      ZQuery2.active:=false;
      ZQuery2.sql.Clear;
      que:='SELECT count(*) FROM '+ZQuery3.Fields[0].AsString+' WHERE TPS='+idtps+' AND STATUS2 = ''0''';
//      ShowMessage(que+' '+nama);
      ZQuery2.sql.Text:=que;
      ZQuery2.Active:=true;
      ZQuery2.first;
      jmlint:=jmlint+ZQuery2.Fields[0].AsInteger;
      Zquery3.Next;
    //tampilan
    end;
    jml:=inttostr(jmlint);
    btps[crow2-1].ID:=idtps;
    btps[crow2-1].NAMA:=nama;
    sgTPS.RowCount:=cRow2;
    sgTPS.Cells[0,crow2-1]:=IntToStr((chal-1)*bhal+crow2-1);
    sgTPS.Cells[1,crow2-1]:=nama;
    sgTPS.Cells[2,crow2-1]:=jml;
    sgTPS.Cells[3,crow2-1]:=alamat;
    inc(crow2);
    zquery1.Next;
  end;
  lblHal.Caption := IntToStr(Hal)+' of '+IntToStr(jHal);
  //-----------------------------------------
  txtHal.Enabled:=True;
  if (cHal=1) then
    btnPrev.Enabled:=False
  else
    btnPrev.Enabled:=True;
  if (cHal=jHal) then
    btnNext.Enabled:=False
  else
    btnNext.Enabled:=True;
  for i := 0 to sgTPS.ColCount - 1 do
    AutoSizeCol(sgTPS, i);
end;

procedure TForm4.getPenduduk();
var
  i:Integer;
  que:String;
begin
    //get penduduk count
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      que:='SELECT count(distinct(RW),RT) FROM pemilih_'+sdesa+' WHERE STATUS2 = ''0'' ORDER BY RW ASC';
      sql.Text:=que;
      Active:=true;
      first;
//      SetLength(bTPS,Fields[0].AsInteger+1);
    end;
    ct2:=ZQuery1.Fields[0].AsInteger;
    jHal2:=ct2 div bHal;
    if ((ct2 mod bHal)>0) then jHal2:=jHal2+1;
    //jpemilih
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      que:='SELECT count(*) FROM pemilih_'+sdesa+' WHERE STATUS2 = ''0''';
      sql.Text:=que;
      Active:=true;
      first;
      lblPemilihTotal.Caption:='Jumlah Pemilih Total : '+Fields[0].AsString;
    end;
    //jpemilihntps
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      que:='SELECT count(*) FROM pemilih_'+sdesa+' WHERE TPS=0 AND STATUS2 = ''0''';
      sql.Text:=que;
      Active:=true;
      first;
      lblPemilihNonTPS.Caption:='Jumlah Pemilih yang Belum Terdaftar Di TPS : '+Fields[0].AsString;
    end;
end;

procedure TForm4.tampilsgpenduduk(Hal:Integer = 1);
var
  que,rw,rt,jml,blm:String;
  i,awal:integer;
begin
//hal
  dRow:=1;
  crow:=2;
  cHal2:=Hal;
  sgPenduduk.RowCount:=crow;
  lblHal2.Caption:='-';
  awal:=(Hal-1)*bHal;
  //reset
  for i := 1 to sgPenduduk.RowCount - 1
  do sgPenduduk.Rows[i].Clear;
  //
  with ZQuery1 do begin
    active:=false;
    sql.Clear;
    que:='SELECT distinct(RW),RT FROM pemilih_'+sdesa+' WHERE STATUS2 = ''0'' ORDER BY RW ASC LIMIT '+IntToStr(awal)+' ,'+IntToStr(bhal);
//    ShowMessage('q1='+que);
    sql.Text:=que;
    Active:=true;
    first;
//    ShowMessage('count='+inttostr(RecordCount));
  end;
  while not zquery1.Eof do begin
    rw:=ZQuery1.Fields[0].AsString;
    rt:=ZQuery1.Fields[1].AsString;
//    showmessage('masuk rw='+rw+' rt='+rt);
    if (rw<>'') and (rt<>'') then begin
        //ganti
        with Zquery2 do begin
          active:=false;
          sql.Clear;
          que:='SELECT count(ID_USER) FROM pemilih_'+sdesa+' WHERE RW='+quotedstr(rw)+' AND RT='+quotedstr(rt)+' AND STATUS2 = 0';
          sql.Text:=que;
          Active:=true;
          first;
          jml:=Fields[0].AsString;
        end;
        with Zquery2 do begin
          active:=false;
          sql.Clear;
          que:='SELECT count(ID_USER) FROM pemilih_'+sdesa+' WHERE RW='+quotedStr(rw)+' AND RT='+quotedStr(rt)+' AND TPS=0 AND STATUS2 = ''0''';
          sql.Text:=que;
          Active:=true;
          first;
          blm:=Fields[0].AsString;
        end;
        //tampilan
        sgPenduduk.RowCount:=cRow;
        sgPenduduk.Cells[0,crow-1]:=IntToStr(crow-1);
        sgPenduduk.Cells[1,crow-1]:=rw;
        sgPenduduk.Cells[2,crow-1]:=rt;
        sgPenduduk.Cells[3,crow-1]:=jml;
        sgPenduduk.Cells[4,crow-1]:=blm;
        inc(crow);
    end;
      zquery1.Next;
  end;
  lblHal2.Caption := IntToStr(Hal)+' of '+IntToStr(jHal2);
  //-----------------------------------------
  txtHal2.Enabled:=True;
  if (cHal2=1) then
    btnPrev2.Enabled:=False
  else
    btnPrev2.Enabled:=True;
  if (cHal2=jHal2) then
    btnNext2.Enabled:=False
  else
    btnNext2.Enabled:=True;
  for i := 0 to sgPenduduk.ColCount - 1 do
    AutoSizeCol(sgPenduduk, i);
end;

procedure TForm4.cbKabkotChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKabkot.ItemIndex > 0) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
      end;
    cbKec.Clear;
    cbKec.Items.Add('PILIH KECAMATAN');
    cbKec.ItemIndex:=0;
    cbDesa.Clear;
    cbDesa.Items.Add('PILIH KELURAHAN/DESA');
    cbDesa.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKabkot:='';
    cbKec.Clear;
    cbDesa.Clear;
  end;
  sKec:='';
  sDesa:='';
  btnRefresh.Enabled:=false;
  ResetMe();
end;

procedure TForm4.cbKecChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKec.ItemIndex > 0) then
  begin
    pilihan := cbKec.Items[cbKec.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+' AND KODE_KABKOT = '+quotedStr(sKabkot);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbDesa.Clear;
    cbDesa.Items.Add('PILIH KELURAHAN/DESA');
    cbDesa.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        cbDesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKec:='';
    cbDesa.Clear;
  end;
  sDesa:='';
  btnRefresh.Enabled:=false;
  ResetMe();
end;

procedure TForm4.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  ext:ExternalFile;
  setting:TSetting_Form;
  i,idx:integer;
  pilihan:string;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ZQuery3.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  //
  with ZTable1 do
  begin
    active := False;
    TableName := 'kabkot';
    Filtered := False;
    active := True;
    first;
  end;
  cbKabkot.Clear;
  i := 0;
  idx:=0;
  while (not ZTable1.Eof) do
  begin
    cbKabkot.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    if (skodekab = ZTable1.FieldByName('KODE').AsString) then
      idx := i;
    ZTable1.Next;
    Inc(i);
  end;
  // ShowMessage(ikabkot);
  cbKabkot.ItemIndex := idx;
  if (skodekab<>'00') then
  begin
    cbkabkot.Enabled:=false;
    if(skodekec = '') then begin
      btnedittps.Visible:=false;
      btnTambahTPS.Visible:=false;
      btnHapus.Visible:=false;
      button2.Visible:=false;
    end;
  end;
  if (skodekec<>'') then cbkec.Enabled:=false;
  sKabkot:='';
  sKec:='';
  sDesa:='';
  cRow:=2;  //wadah row
  dRow:=1;  //menyimpan banyaknya data
  cRow2:=2;  //wadah row
  dRow2:=1;  //menyimpan banyaknya data
  bHal:=5;  //pemilih per halaman
  lblHal.Caption:= '-';
  txtHal.Enabled:=False;
  btnPrev.Enabled:=False;
  btnNext.Enabled:=False;
  //
  lblHal2.Caption:= '-';
  txtHal2.Enabled:=False;
  btnPrev2.Enabled:=False;
  btnNext2.Enabled:=False;
  //grid 1
  btnRefresh.Enabled:=false;
  sgPenduduk.Cells[0,0]:='NO';
  sgPenduduk.Cells[1,0]:='RW';
  sgPenduduk.Cells[2,0]:='RT';
  sgPenduduk.Cells[3,0]:='Jumlah Penduduk';
  sgPenduduk.Cells[4,0]:='Belum Masuk TPS';
  sgPenduduk.RowCount:=cRow;
  for i := 0 to sgPenduduk.ColCount - 1 do
    AutoSizeCol(sgPenduduk, i);
  //grid 2
  sgTPS.Cells[0,0]:='NO';
  sgTPS.Cells[1,0]:='Nama TPS';
  sgTPS.Cells[2,0]:='Jumlah Pemilih';
  sgTPS.Cells[3,0]:='Alamat TPS';
  sgTPS.RowCount:=cRow2;
  for i := 0 to sgTPS.ColCount - 1 do
    AutoSizeCol(sgTPS, i);
    
  //bpenyimpan
  with ZTable1 do begin
    Active:=False;
    TableName:='penyimpan';
    Active:=True;
    SetLength(bPenyimpan,RecordCount);
    I:=0;
    while (not Eof) do begin
      bPenyimpan[I].ID:=FieldByName('ID').AsString;
      bPenyimpan[I].NAMA:=FieldByName('NAMA').AsString;
      Inc(I);
      Next;
    end;
  end;
  
  //
  lblNTPS.Caption:='';
  lblPemilihTPS.Caption:='';
  lblPemilihTotal.Caption:='';
  lblPemilihNonTPS.Caption:='';
  if (cbKabkot.ItemIndex > -1) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
      end;
    cbkec.Clear;
    cbDesa.Clear;
    cbkec.Items.Add('PILIH KECAMATAN');
    idx:=0;
    while (not ZTable1.Eof) do
      begin
        cbkec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        idx:=idx+1;
        if(skodekec=ZTable1.FieldByName('KODE_KECAMATAN').AsString)then
          cbkec.itemindex:=idx;
        ZTable1.Next;
      end;
    end;
  if (cbKec.ItemIndex > -1) then
  begin
    pilihan := cbKec.Items[cbKec.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+' AND KODE_KABKOT = '+quotedStr(sKabkot);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbdesa.Clear;
    cbdesa.Items.Add('PILIH DESA');
    while (not ZTable1.Eof) do
      begin
        cbdesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end;
  if(Date < EncodeDate( 2012, 10, 10 )) then
  begin
//    showMessage('tutup');
     btnTambahTPS.Visible := false;
     btnEdittps.Visible := false;
     btnHapus.Visible := false;
  end;
  if(Date > EncodeDate( 2013, 2, 25 )) then
  begin
//    showMessage('tutup');
     btnTambahTPS.Visible := false;
     btnEdittps.Visible := false;
     btnHapus.Visible := false;
  end;
end;

function TForm4.getIdsimpan(tabel:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].NAMA=tabel) then begin
      res:=bPenyimpan[i].ID;
      break;
    end;
  end;
    Result:=res;
end;

function TForm4.getSimpan(id:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].ID=id) then begin
      res:=bPenyimpan[i].NAMA;
      break;
    end;
  end;
    Result:=res;
end;

procedure TForm4.txtHalKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_RETURN then
    getPage;
end;



procedure TForm4.getPage();
begin
    if ((txtHal.Text='') or (StrToInt(txtHal.Text)>jHal) or (StrToInt(txtHal.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalaman(StrToInt(txtHal.Text));
      end;
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
  tambahntps:=TForm_TambahTPS.Create(Application,sdesa,2);
end;

procedure TForm4.Button3Click(Sender: TObject);
var
  query, kode_tps:String;
  no_terakhir, no_temp:integer;
  buttonSelected : Integer;
begin
  no_terakhir:=0;
  no_temp:=0;
  buttonSelected := MessageDlg('Apakah anda yakin akan menambah TPS Khusus?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    //lihat nomor sekarang
    query:='SELECT * FROM daftar_tps WHERE NAMA LIKE ''K%'' AND KODE_DESA = '+quotedStr(sdesa);
    with zquery1 do begin
      active := false;
      SQL.Clear;
      SQL.Text:=query;
      active := true;
      First;
      while not eof do begin
        no_temp := strtoint(copy(fieldByName('NAMA').AsString,2,2));
        if(no_temp>no_terakhir) then
          no_terakhir:=no_temp;
        Next;
      end;
    end;

    //diincrement
    no_terakhir := no_terakhir+1;
    kode_tps := inttostr(no_terakhir);
    if(length(kode_tps)=1) then
      kode_tps := '0'+kode_tps;
    kode_tps := 'K'+kode_tps;

    //dimasukin
    query:=('INSERT INTO daftar_tps(NAMA, KODE_DESA, ALAMAT) VALUES('+quotedStr(kode_tps)+','+quotedStr(sdesa)+','''')');
    with zquery1 do begin
      active := false;
      SQL.Clear;
      SQL.Text:=query;
      execSql;
    end;

    showmessage('berhasil menambahkan TPS Khusus');

    if getIdsimpan('pemilih_'+sdesa)<>'' then begin
      //getpenduduk;
      //if (ct2>=0) then tampilsgpenduduk(1);
      //
      getData;
      if (ct>=0) then tampilhalaman(1);
      //
      btnTambahTPS.Enabled:=True;
      button2.enabled:=true;
    end
  end;
end;

end.
